﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using HotelSoft.HotelManagement_BUS;
using HotelSoft.HotelManagement_DTO;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmMain_Tieptan : Form
    {
        PhongBUS busPhong = new PhongBUS();
        LoaiphongBUS busLoaiphong = new LoaiphongBUS();
        PhongDTO DtoPhong = new PhongDTO();
        mainTiepTanBUS busmainTT = new mainTiepTanBUS();
        Double temp = 1;
        public frmMain_Tieptan()
        {
            InitializeComponent();
        }

        private void frmMain_Tieptan_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            nvPannel.Expanded = true;
            LoadCombobox();
            if (cboLoaiPhong.Items.Count != 0)
            {
                cboLoaiPhong.SelectedIndex = 0;
                DataTable tbl = new DataTable();
                tbl = busPhong.dataTablegetListPhong(cboLoaiPhong.SelectedValue.ToString());
                LoadGridview(tbl);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChangePass_Click(object sender, EventArgs e)
        {
            frmChangePassword fr = new frmChangePassword();
            fr.ShowDialog();
        }

        private void btnChangepass_Click(object sender, DevComponents.DotNetBar.ClickEventArgs e)
        {
            frmChangePassword fr = new frmChangePassword();
            fr.ShowDialog();
        }

        private void btnInfo_Click(object sender, DevComponents.DotNetBar.ClickEventArgs e)
        {
            frmInfo fr = new frmInfo();
            fr.ShowDialog();
        }

        private void btnLogout_Click(object sender, DevComponents.DotNetBar.ClickEventArgs e)
        {
            this.Close();
        }

        private void btnClose_Click(object sender, DevComponents.DotNetBar.ClickEventArgs e)
        {
            Application.Exit();
        }

        public void LoadCombobox()
        {
            DataTable tbl = new DataTable();
            tbl = busLoaiphong.SelectAll();
            cboLoaiPhong.DisplayMember = "TenLP";
            cboLoaiPhong.ValueMember = "TenLP";
            cboLoaiPhong.DataSource = tbl;
        }

        public void LoadGridview(DataTable tbl)
        {
            LVPhong.Items.Clear();
            ListViewItem item;
            foreach (DataRow dr in tbl.Rows)
            {
                item = new ListViewItem();
                if (dr["TrangThai"].ToString() == "Trống")
                    item.ImageIndex = 0;
                else
                    item.ImageIndex = 1;
                item.Text=dr["SoPhong"].ToString();
                item.SubItems.Add(dr["SoKhachToiDa"].ToString());
                item.SubItems.Add(dr["TrangThai"].ToString());
                item.SubItems.Add(dr["TenLP"].ToString());
                item.SubItems.Add(dr["Gia"].ToString());
                LVPhong.Items.Add(item);
            }
        }

        public int LayTinhTrangPhong(string sTinhTrang)
        {
            ThamSo ThamSo = new ThamSo();
            if (sTinhTrang == ThamSo.dsTinhTrangPhong[1].ToString())
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        private void cboLoaiPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLoaiPhong.SelectedIndex != -1)
            {
                DataTable tb = new DataTable();
                tb = busPhong.dataTablegetListPhong(cboLoaiPhong.SelectedValue.ToString());
                LoadGridview(tb);

                DataTable tbl = new DataTable();
                LoaiPhongDTO dto = new LoaiPhongDTO();
                dto.TENLP = cboLoaiPhong.SelectedValue.ToString();
                tbl = busLoaiphong.SelectAll_DieuKien(dto);
                Decimal gia = 0;
                gia = Decimal.Parse(tbl.Rows[0].ItemArray[1].ToString());
                lblGia.Text = gia.ToString("#,##0") + " VNĐ";
            }
        }

        private void LVPhong_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
        {
            DtoPhong = new PhongDTO();
            DtoPhong.SOPHONG = e.Item.SubItems[0].Text;
            DtoPhong.SOKHACHTOIDA = int.Parse(e.Item.SubItems[1].Text);
            DtoPhong.TRANGTHAI = e.Item.SubItems[2].Text;
            DtoPhong.TENLP = e.Item.SubItems[3].Text;
            DtoPhong.DONGIA = float.Parse(e.Item.SubItems[4].Text);
            if (DtoPhong.TRANGTHAI == "Trống")
            {
                stPhong.SetSuperTooltip(LVPhong, new DevComponents.DotNetBar.SuperTooltipInfo("THÔNG TIN PHÒNG", "<i>Hãy nhấp đôi vào phòng để thực hiện chức năng</i>", "Số phòng: <b>" + DtoPhong.SOPHONG + "</b><br/>Giá: " + DtoPhong.DONGIA.ToString("#,##0 VNĐ") + "<br/>Tình trạng: <b>" + DtoPhong.TRANGTHAI + "</b><br/>Số khách tối đa: <b>" + DtoPhong.SOKHACHTOIDA.ToString() + "</b>", global::HotelSoft.Properties.Resources.Room2, null, DevComponents.DotNetBar.eTooltipColor.Gray));
                stPhong.ShowTooltip(sender);
            }
            else
            {
                DataTable tbl = new DataTable();
                tbl = busmainTT.getSoPDK(DtoPhong);
                PhieuDangKyDTO dtoPDK = new PhieuDangKyDTO();
                dtoPDK.SoNgayThue = int.Parse(tbl.Rows[0].ItemArray[2].ToString());
                dtoPDK.Ngayden = DateTime.Parse(tbl.Rows[0].ItemArray[3].ToString());
                dtoPDK.SoNguoi = int.Parse(tbl.Rows[0].ItemArray[4].ToString());
                stPhong.SetSuperTooltip(LVPhong, new DevComponents.DotNetBar.SuperTooltipInfo("THÔNG TIN PHÒNG", "<i>Hãy nhấp đôi vào phòng để thực hiện chức năng</i>", "Số phòng: <b>" + DtoPhong.SOPHONG + "</b><br/>Giá: " + DtoPhong.DONGIA.ToString("#,##0 VNĐ") +  "<br/>Số người: <b>" + dtoPDK.SoNguoi.ToString() + "</b><br/>Ngày thuê: <b>" + dtoPDK.Ngayden.ToString("dd/MM/yyyy") + "</b><br/>Số ngày thuê: <b>" + dtoPDK.SoNgayThue + "</b> <i>(dự định)</i>", global::HotelSoft.Properties.Resources.Room2, null, DevComponents.DotNetBar.eTooltipColor.Gray));
                stPhong.ShowTooltip(sender);
            }
        }

        private void lklQL_KH_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmQuanly_KHACHHANG fr = new frmQuanly_KHACHHANG();
            fr.ShowDialog();
        }

        private void LVPhong_DoubleClick(object sender, EventArgs e)
        {
            DtoPhong = new PhongDTO();
            DtoPhong.SOPHONG = LVPhong.FocusedItem.SubItems[0].Text;
            DtoPhong.TRANGTHAI = LVPhong.FocusedItem.SubItems[2].Text;
            if (DtoPhong.TRANGTHAI=="Trống")
            {
                frmDat_PHONG fr = new frmDat_PHONG();
                fr.PHONG = DtoPhong.SOPHONG;
                fr.ShowDialog();
                fr.FormClosing += new FormClosingEventHandler(fr_FormClosing);
                cboLoaiPhong_SelectedIndexChanged(sender, e);
            }
            else
            {
                DataTable tbl = new DataTable();
                tbl = busmainTT.getSoPDK(DtoPhong);
                if (tbl.Rows.Count == 0)
                {
                    MessageBox.Show("Tình trạng phòng đang thuê\nTuy nhiên chưa có phiếu đăng ký\nXin vui lòng kiểm tra lại!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    return;
                }
                frmTra_PHONG frm = new frmTra_PHONG();
                frm.SOPHIEUDK = tbl.Rows[0].ItemArray[0].ToString();
                frm.ShowDialog();
                frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                cboLoaiPhong_SelectedIndexChanged(sender, e);
            }
        }

        void fr_FormClosing(object sender, FormClosingEventArgs e)
        {
            cboLoaiPhong_SelectedIndexChanged(sender, e);
        }

        void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            cboLoaiPhong_SelectedIndexChanged(sender, e);
        }

        private void LVPhong_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            LVPhong.Items[e.Index].BackColor = Color.Yellow;
        }

        private void lklSearch_KH_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmTraCuu_KhachHang frm = new frmTraCuu_KhachHang();
            frm.ShowDialog();
        }

        private void Btn_Tracuu_Click(object sender, EventArgs e)
        {
            frmTraCuu_Phong frm = new frmTraCuu_Phong();
            frm.TIEPTAN = true;
            frm.ShowDialog();
            frm.FormClosing+=new FormClosingEventHandler(frm_FormClosing);
        }

        private void lklBaocao_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmBaocao_NGAY frm = new frmBaocao_NGAY();
            frm.ShowDialog();
        }

        private void bubbleButton3_Click(object sender, DevComponents.DotNetBar.ClickEventArgs e)
        {
            Process.Start("HuongDan.chm");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
                timer1.Enabled = false;
        }
    }
}
